package com.yykj.hospital.server.controller;

import com.yykj.common.enums.BanCiType;
import com.yykj.common.enums.DoctorArticleShowClient;
import com.yykj.common.enums.HospitalOnlineTypeEnum;
import com.yykj.common.vo.PageData;
import com.yykj.common.vo.ResultVO;
import com.yykj.hospital.client.dto.DoctorArticleDTO;
import com.yykj.hospital.client.dto.DoctorArticleDetailDTO;
import com.yykj.hospital.server.dto.banci.DoctorVisitLevelDTO;
import com.yykj.hospital.server.dto.banci.patient.client.AppointDayInfo;
import com.yykj.hospital.server.dto.banci.patient.client.AppointDayTimeInfo;
import com.yykj.hospital.server.dto.banci.patient.client.DoctorBanCiListDTO;
import com.yykj.hospital.server.dto.train.FormTrainLogListDTO;
import com.yykj.hospital.server.form.ArticleSearchForm;
import com.yykj.hospital.server.form.BanCiSearchForm;
import com.yykj.hospital.server.form.train.FormTrainContent;
import com.yykj.hospital.server.form.train.FormTrainLogSearchForm;
import com.yykj.hospital.server.form.train.FormTrainSearchForm;
import com.yykj.hospital.server.model.entity.HospitalSettingAppointDialog;
import com.yykj.hospital.server.model.entity.form.template.FormTrain;
import com.yykj.hospital.server.model.entity.form.template.FormTrainDetail;
import com.yykj.hospital.server.service.ArticleService;
import com.yykj.hospital.server.service.HospitalSettingService;
import com.yykj.hospital.server.service.banci.BanCiService;
import com.yykj.hospital.server.service.banci.PatientClientBanCiService;
import com.yykj.hospital.server.service.from.template.FormTrainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/hospital/patient/client")
@Api(tags = {"患者端 APP 需要的接口"})
@SuppressWarnings("unchecked")
public class HospitalPatientClientController {
    @ApiOperation("预约挂号 -- 预约需知")
    @GetMapping("/appointment/dialog/{hospitalId}")
    public ResultVO<HospitalSettingAppointDialog> viewHospitalSettingAppointDialog(@PathVariable Integer hospitalId) {
        return ResultVO.success(hospitalSettingService.viewHospitalSettingAppointDialog(hospitalId));
    }


    @ApiOperation("健康科普 -- 文章-- 列表")
    @GetMapping("/doctor/article/list")
    public ResultVO<PageData<DoctorArticleDTO>> listDoctorArticle(@RequestParam(name = "hospitalId") Integer hospitalId,
                                                                  @RequestParam(name = "doctorId", required = false) String doctorId,
                                                                  @RequestParam(name = "keyword", required = false) String keyword,
                                                                  @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {


        ArticleSearchForm form = ArticleSearchForm.builder()
                .hospitalId(hospitalId)
                .doctorId(doctorId)
                .keyword(keyword)
                .currentPage(currentPage)
                .pageSize(pageSize)
                .client(DoctorArticleShowClient.patient)
                .searchType(4)
                .status(true)
                .build();

        return ResultVO.success(articleService.listArticle(form));

    }

    @ApiOperation("健康科普 -- 文章--查看")
    @GetMapping("/doctor/article/view/{id}")
    public ResultVO<DoctorArticleDetailDTO> viewArticleDetail(@PathVariable Integer id) {
        return ResultVO.success(articleService.viewArticleDetail(id));
    }

    @ApiOperation("预约挂号 -- 可挂号医生列表")
    @GetMapping("/appointment/available")
    public ResultVO<List<DoctorBanCiListDTO>> listPatientClientDoctorBanCi(@RequestParam(name = "hospitalId") Integer hospitalId,
                                                                           @RequestParam(name = "depId") Integer depId,
                                                                           @RequestParam(name = "levelId", required = false) Integer levelId,
                                                                           @RequestParam(name = "available", required = false) Boolean available) {

        BanCiSearchForm form = BanCiSearchForm.builder()
                .hospitalId(hospitalId)
                .depId(depId)
                .levelId(levelId)
                .available(available)
                .baseTime(LocalDateTime.now())
                .type(BanCiType.appointment)
                .build();
        return ResultVO.success(patientClientBanCiService.patientClientViewAppointmentBanCi(form));
    }

    @ApiOperation("预约挂号 -- 获取排班日历明细")
    @GetMapping("/appointment/calendar/info")
    public ResultVO<List<AppointDayInfo>> listDoctorAppointDayInfo(@RequestParam(name = "doctorId") String doctorId,
                                                                   @RequestParam(name = "levelId", required = false) Integer levelId) {
        return ResultVO.success(patientClientBanCiService.listDoctorAppointDayInfo(doctorId, levelId));
    }

    @ApiOperation("预约挂号 -- 获取具体时间明细")
    @GetMapping("/appointment/time/info")
    public ResultVO<List<AppointDayTimeInfo>> listAppointDayTimeInfo(@RequestParam(name = "doctorId") String doctorId,
                                                                     @RequestParam(name = "day") String day) {
        return ResultVO.success(patientClientBanCiService.listAppointDayTimeInfo(doctorId, day));
    }


    @ApiOperation("在线问诊 -- 获取排班日历明细")
    @GetMapping("/online/calendar/info")
    public ResultVO<List<AppointDayInfo>> listDoctorOnlineDayInfo(@RequestParam(name = "doctorId") String doctorId,
                                                                  @RequestParam(name = "type") HospitalOnlineTypeEnum type) {
        return ResultVO.success(patientClientBanCiService.listDoctorOnlineDayInfo(doctorId, type));
    }

    @ApiOperation("在线问诊 -- 获取具体时间明细")
    @GetMapping("/online/time/info")
    public ResultVO<List<AppointDayTimeInfo>> listOnlineDayTimeInfo(@RequestParam(name = "doctorId") String doctorId,
                                                                    @RequestParam(name = "day") String day,
                                                                    @RequestParam(name = "type") HospitalOnlineTypeEnum type) {
        return ResultVO.success(patientClientBanCiService.listOnlineDayTimeInfo(doctorId, day,type));
    }


    @ApiOperation("获取出诊级别列表 ")
    @GetMapping("/level/list")
    public ResultVO<List<DoctorVisitLevelDTO>> listLevel(@RequestParam(name = "hospitalId") Integer hospitalId) {
        return ResultVO.success(banCiService.listDoctorVisitLevel(hospitalId, true, null));
    }

    //================直通车相关接口===========================================================

    @ApiOperation("直通车 -- 明细")
    @GetMapping("/form/train/detail/{id}")
    public ResultVO<List<FormTrainDetail>> listFormTrainDetail(@PathVariable Integer id) {
        return ResultVO.success(formTrainService.listFormTrainDetail(id));
    }

    @ApiOperation("直通车 -- 提交表单")
    @PostMapping("/form/train/submit")
    public ResultVO<List<FormTrainDetail>> submitTrain(@RequestBody FormTrainContent content) {
        formTrainService.submitTrain(content);
        return ResultVO.success();
    }

    @ApiOperation("直通车 -- 用户反馈 -- 列表")
    @GetMapping("/form/train/feedback/list")
    public ResultVO<PageData<FormTrainLogListDTO>> listFormTrainFeedbackLog(FormTrainLogSearchForm form) {
        return ResultVO.success(formTrainService.listFormTrainFeedbackLog(form));
    }


    private FormTrainService formTrainService;

    private PatientClientBanCiService patientClientBanCiService;

    private HospitalSettingService hospitalSettingService;

    private ArticleService articleService;

    private BanCiService banCiService;

    @Autowired
    public void setHospitalSettingService(HospitalSettingService hospitalSettingService) {
        this.hospitalSettingService = hospitalSettingService;
    }

    @Autowired
    public void setArticleService(ArticleService articleService) {
        this.articleService = articleService;
    }

    @Autowired
    public void setPatientClientBanCiService(PatientClientBanCiService patientClientBanCiService) {
        this.patientClientBanCiService = patientClientBanCiService;
    }

    @Autowired
    public void setBanCiService(BanCiService banCiService) {
        this.banCiService = banCiService;
    }
    @Autowired
    public void setFormTrainService(FormTrainService formTrainService) {
        this.formTrainService = formTrainService;
    }
}
